/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2013-10-15         CRuby
 */

package com.skyworth.badsdk;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.skyworth.framework.skysdk.logger.Logger;

/**
 * <p>Description:</p>
 * <p>下载相关信息数据结构</p>
 *
 * @author CRuby
 * @version V1.0.0
 * @ClassName SkyRecordInfo
 * @date 2013-10-21
 */
public class SkyRecordInfo {
    /**
     * Description:表示5条记录
     */
    private static int gCount = 5;

    /**
     * Description:字段分割符
     */
    private final static String gDelimiter = ":>>>";

    /**
     * Description:换行符
     */
    private final static String gEndLine = "\n";

    /**
     * Description:已经下载的长度
     */
    public int realLength = 0;
    /**
     * Description:文件总长度
     */
    public int completeLength = 0;
    /**
     * Description:文件原始名称
     */
    public String fileName = "";
    /**
     * Description:下载的url
     */
    public String url = "";
    /**
     * Description:最后修改日期
     */
    public String modifyDate = "";

    /**
     * TODO(构造函数，默认自动生成修改日期)
     */
    public SkyRecordInfo() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        modifyDate = format.format(new Date());
    }

//    public SkyRecordInfo(String info)
//    {
//        Logger.d(info);
//        SkyRecordInfo newT = JSON.parseObject(info, new TypeReference<SkyRecordInfo>()
//        {
//        });
//        realLength = newT.realLength;
//        fileName = newT.fileName;
//        url = newT.url;
//        modifyDate = newT.modifyDate;
//    }

//    @Override
//    public String toString()
//    {
//        JSONObject t_json = new JSONObject();
//        t_json.put("realLength", realLength);
//        t_json.put("fileName", fileName);
//        t_json.put("url", url);
//        t_json.put("modifyDate", modifyDate);
//        return t_json.toJSONString();
//    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        String content = "";
        content += new String("file" + gDelimiter + fileName);
        content += gEndLine;
        content += new String("real length" + gDelimiter + realLength);
        content += gEndLine;
        content += new String("url" + gDelimiter + url);
        content += gEndLine;
        content += new String("modify" + gDelimiter + modifyDate);
        content += gEndLine;
        content += new String("complete length" + gDelimiter + completeLength);
        content += gEndLine;
        return content;
    }

    /**
     * 反序列化，与  toString 相对应
     *
     * @param seria
     */
    public SkyRecordInfo(String seria) {
        if (seria != null) {
            String[] strs = seria.split(gEndLine);
            if (strs.length >= gCount) {
                String[] index0 = strs[0].split(gDelimiter);
                if (2 == index0.length) {
                    fileName = index0[1];
                }
                String[] index1 = strs[1].split(gDelimiter);
                if (2 == index1.length) {
                    if (index1[1] != null) {
                        realLength = Integer.parseInt(index1[1]);
                    }
                }
                String[] index2 = strs[2].split(gDelimiter);
                if (2 == index2.length) {
                    url = index2[1];
                }
                String[] index3 = strs[3].split(gDelimiter);
                if (2 == index3.length) {
                    modifyDate = index3[1];
                }
                String[] index4 = strs[4].split(gDelimiter);
                if (2 == index4.length) {
                    completeLength = Integer.parseInt(index4[1]);
                }
            }
        }
    }

    /**
     * 概述：Java 测试SkyRecordInfo的序列化和反序列化的正确性<br/>
     *
     * @param args void
     * @date 2013-10-21
     */
    public static void main(String[] args) {
        long count1 = System.currentTimeMillis();
        SkyRecordInfo info = new SkyRecordInfo();
        info.realLength = 55566;
        info.completeLength = 1000000;
        info.fileName = "gsf";
        info.modifyDate = "2013年10月15日16:37:21";
        info.url = "http://www.hao123.com";
        Logger.i(info.toString());
        long count2 = System.currentTimeMillis();
        System.out.println(count2 - count1);

        SkyRecordInfo info2 = new SkyRecordInfo(info.toString());
        Logger.d(String.valueOf(info2.realLength));
        Logger.d(info2.url);
        Logger.d(info2.modifyDate);
        Logger.d(info2.fileName);
        Logger.d(String.valueOf(info2.completeLength));
        long count3 = System.currentTimeMillis();
        System.out.println(count3 - count2);
    }
}
